'''
authot:leemars
time:2022/7/22
langulage:python
'''

from tkinter import Image
import pyautogui
import schedule
import time
import requests
import json
import pytesseract
from PIL import Image,ImageFilter
import sys

 
# 获取鼠标位置
def get_mouse_positon():
  #time.sleep(5) # 准备时间
  print('开始获取鼠标位置')
  try:
    for i in range(10):
      # Get and print the mouse coordinates.
      x, y = pyautogui.position()
      positionStr = '鼠标坐标点（X,Y）为：{},{}'.format(str(x).rjust(4), str(y).rjust(4))
      #positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(
        #3) + ')'
      print(positionStr)
      time.sleep(0.5) # 停顿时间
  except:
    print('获取鼠标位置失败')
  
#消息推送函数
def sendsuccess():
  headers={
    "User-Agent":"Mozilla/5.0 (Linux; Android 9; SM-A102U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.93 Mobile Safari/537.36",
    'Content-Type': 'application/json'
  }
  url='http://www.pushplus.plus/send'
  data={'token':'099c938e16f3466283cfff2a509c7cb7','title':'占座成功通知','content':'恭喜你，占座成功！','template':'html'}
  res=requests.post(headers=headers,url=url,data=json.dumps(data),timeout=10)
  print(res.status_code)
  print(res.text)
  print("占座成功！")

#防止掉线函数
def entermessage():
  pyautogui.FAILSAFE = True#避免鼠标越界导致程序自动停止
  pyautogui.typewrite('normal')
  pyautogui.hotkey('ctrl', 'enter')

# 程序执行函数
def reverseat():
    print('开始占座')
    #planA 
    #pyautogui.hotkey('alt', 'tab',duration=0.05)#切换抢座页面
    #pyautogui.click(x=123, y=41, duration=0.01)#鼠标单击刷新按钮
    #planB
    pyautogui.hotkey('up',duration=0.05)#切换抢座公众号
    pyautogui.click(x=439, y=1013, duration=0.1)#点击选座按钮
    #进入页面
    pyautogui.click(x=273,y=875, duration=0.5)#鼠标点击首选位置
    pyautogui.click(x=785,y=876, duration=0.3)#鼠标点击备选位置
    #判断抢座是否成功
    time.sleep(3)
    pix = pyautogui.pixel(343, 667) # 获取指定区域RGB颜色
    #print(pix)
    print("已完成选座所有步骤（无论成功与否）")
    if pix == (0,0,0):
      sendsuccess()
    sys.exit()


#定时主函数
time.sleep(10)#操作时间
entermessage()#判断程序是否开始
schedule.every(1200).seconds.do(entermessage)#相隔20min发送一条消息
schedule.every().day.at("05:30").do(reverseat)#5:30准时抢座
while True:
    schedule.run_pending()
    time.sleep(0.5)

#测试区域
#get_mouse_positon() #获取鼠标位置
#reverseat()
#pix = pyautogui.pixel(104, 779) # 获取指定区域RGB颜色
#print(pix)
#pyautogui.screenshot(r'C:\Users\Administrator\Desktop\region_screenshot.png', region=(219, 478, 175, 135))
#path = 'C:/Users/Administrator/Desktop/test.png'
#test = Image.open(path)
#result = pytesseract.image_to_string(test)
#print(result)